package com.zhuss.outer.outer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.outer.outer.DetailOuterBo;
import com.zhuss.bean.bo.outer.outer.SearchOuterBo;
import com.zhuss.bean.bo.outer.outer.wechat.ListOuterBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.outer.outer.DetailOuterVo;
import com.zhuss.bean.vo.outer.outer.ListOuterVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.outer.processor.OuterAppendRead;
import com.zhuss.outer.processor.OuterRead;
import com.zhuss.outer.processor.OuterTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * outer查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("outerReadService")
public class OuterReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("outerRead")
	private OuterRead outerRead;

	@Autowired
	@Qualifier("outerTopicRead")
	private OuterTopicRead outerTopicRead;
	
	@Autowired
	@Qualifier("outerAppendRead")
	private OuterAppendRead outerAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailOuterBo bo = (DetailOuterBo) param;
		Long outerId = bo.getOuterId();
		DetailOuterVo vo = (DetailOuterVo) outerRead.detail(outerId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchOuterBo bo = (SearchOuterBo) param;
		Object result = outerRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailOuterBo param) throws WpsException {
		Long outerId = param.getOuterId();
		Object result = outerTopicRead.listTopicTree(outerId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listOuter(Object param) throws WpsException {
		ListOuterBo bo = (ListOuterBo) param;
		Object result = outerRead.listOuter(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailOuterVo> pageVo = (PageVo<DetailOuterVo>) result;
		for (DetailOuterVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = outerAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOuterId(), null).size();
			Integer collectNum = outerAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOuterId(), null).size();
			Integer forwardNum = outerAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOuterId(), null).size();
			Integer topicNum = outerTopicRead.listTopic(vo.getOuterId(), 0l).size();
			
			Integer thumbSelfNum = outerAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOuterId(), userId).size();
			Integer collectSelfNum = outerAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOuterId(), userId).size();
			Integer forwardSelfNum = outerAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOuterId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListOuterVo> pageVo = (PageVo<ListOuterVo>) result;
		for (ListOuterVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = outerAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOuterId(), null).size();
			Integer collectNum = outerAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOuterId(), null).size();
			Integer forwardNum = outerAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOuterId(), null).size();
			Integer topicNum = outerTopicRead.listTopic(vo.getOuterId(), 0l).size();
			
			Integer thumbSelfNum = outerAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOuterId(), userId).size();
			Integer collectSelfNum = outerAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOuterId(), userId).size();
			Integer forwardSelfNum = outerAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOuterId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			
		}
	}
	
	
}

